package com.ssd.service.impl;

import com.ssd.common.VMSystem;
import com.ssd.mapper.OrderCollectMapper;
import com.ssd.mapper.OrderMapper;
import com.ssd.entity.OrderCollectEntity;

import com.ssd.service.ReportService;
import com.ssd.viewmodel.BarCharCollect;
import com.ssd.viewmodel.Pager;
import com.ssd.vo.CollectBillVO;
import com.ssd.vo.CollectOrderVO;
import com.ssd.vo.RegionTotalVO;
import com.ssd.vo.SkuSeniority;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

/**
 * ReportServiceImpl 类
 *
 * @title:
 * @requirement:
 * @ClassName:
 * @author:首雯静
 * @creationTime:2024-11-28/15:20
 * @version: 1.0
 */

@Service
public class ReportServiceImpl implements ReportService {

    @Resource
    private OrderMapper orderMapper;//订单列表数据访问对象
    @Resource
    private OrderCollectMapper orderCollectMapper;//订单汇总表数据访问对象

    @Override
    public Pager<OrderCollectEntity> getPartnerCollect(Long pageIndex, Long pageSize, String name, LocalDate start, LocalDate end) {
        return null;
    }

    @Override
    public List<OrderCollectEntity> getTop12(Integer partnerId) {
        return null;
    }

    @Override
    public Pager<OrderCollectEntity> search(Long pageIndex, Long pageSize, Integer partnerId, String nodeName, LocalDate start, LocalDate end) {
        return null;
    }

    @Override
    public List<OrderCollectEntity> getList(Integer partnerId, String nodeName, LocalDate start, LocalDate end) {
        return null;
    }

    @Override
    public BarCharCollect getCollect(Integer partnerId, LocalDate start, LocalDate end) {
        List<CollectBillVO> collectBillList = orderCollectMapper.getCollect(partnerId, start, end);
        BarCharCollect result = new BarCharCollect();//返回给前端图形的对象
        if (collectBillList.size() == 0 || collectBillList != null){
            //如果没查询到值，则直接返回空对象
            return result;
        }
        //将查询出的数据封装到返回对象中
        for (CollectBillVO collectBillVo : collectBillList) {
            result.getXAxis().add(collectBillVo.getDate());//x轴存储分段的日期
            result.getSeries().add(collectBillVo.getTotalBill());//y轴存储具体收益
        }
        return result;
    }

    @Override
    public BarCharCollect getAmountCollect(LocalDate start, LocalDate end) {
        List<CollectBillVO> amountCollect = orderMapper.getAmountCollect(start, end, VMSystem.PAY_STATUS_PAYED);
        BarCharCollect result = new BarCharCollect();//图形数据对象
        for (CollectBillVO collectBillVo : amountCollect) {
            result.getXAxis().add(collectBillVo.getDate());//x轴保存日期
            result.getSeries().add(collectBillVo.getTotalBill());//y轴保存具体销售额
        }
        return result;
    }

    @Override
    public BarCharCollect getCollectByRegion(LocalDate start, LocalDate end) {
        //查询已经支付完毕的订单
        List<RegionTotalVO> collectByRegion = orderMapper.getCollectByRegion(start, end, VMSystem.PAY_STATUS_PAYED);
        BarCharCollect result = new BarCharCollect();//返回给前端图形的对象
        if (collectByRegion.size() == 0 || collectByRegion != null){
            //如果没查询到值，则直接返回空对象
            return result;
        }
        //将查询出的数据封装到返回对象中
        for (RegionTotalVO regionTotalVO : collectByRegion) {
            // TODO 查询该区域的具体名称
            result.getSeries().add(regionTotalVO.getTotalAmount());//y轴存储具体销售额
        }
        return result;
    }

    @Override
    public Integer getTotalAmount(LocalDate start, LocalDate end) {
        return orderMapper.getTotalAmount(start, end, VMSystem.ORDER_STATUS_PAYED);
    }

    @Override
    public Integer getOrderCount(LocalDate start, LocalDate end) {
        //查询支付完成的订单
        return orderMapper.getOrderCount(start, end, VMSystem.ORDER_STATUS_PAYED);
    }

    @Override
    public List<SkuSeniority> getSkuSeniority(LocalDate start, LocalDate end) {
        return orderMapper.getSkuSeniority(start, end, VMSystem.ORDER_STATUS_PAYED);
    }

    @Override
    public BarCharCollect getOrderCollect(LocalDate start, LocalDate end) {
        List<CollectOrderVO> orderCollect = orderMapper.getOrderCollect(start, end, VMSystem.ORDER_STATUS_PAYED);
        //返回给前端图形的对象
        BarCharCollect barCharCollect = new BarCharCollect();
        //获取当前日期月份
        int monthValue = LocalDate.now().getMonthValue();
        //将查询的值保存到图形对象中
        for (CollectOrderVO collectOrderVO : orderCollect) {
            //判断是否为本月的销售量
            if(collectOrderVO.getMonth() == monthValue){
                barCharCollect.getXAxis().add("本月");
                barCharCollect.getSeries().add(collectOrderVO.getCount());
            }else{
                barCharCollect.getXAxis().add("上个月");
                barCharCollect.getSeries().add(collectOrderVO.getCount());
            }
        }
        return barCharCollect;
    }
}
